<?php
  class iJJsTreeOperations{
    const DOMAIN          = '2';
    const CORE            = '3';
    const BLOCKS          = '4';
    const ST              = '5';
    const CLASSES         = '6';
    const BACKEND_BLOCKS  = '7';
    const FRONTEND_BLOCKS = '9';
    
    
    
    public static function get_children($node) {
      
        $response = array();
        if($node['id'] == 1) {
          $obj['attr']  = array('id' => 'node_' . self::DOMAIN, 'rel' => 'drive');
          $obj['data']  = DOMAIN_NAME;
          $obj['state'] = 'closed';
          $response[] = $obj;
        } 
        
        if($node['id'] == self::DOMAIN) {
          $name = str_replace(DIR_FS_ROOT, '' ,DIR_CORE);
          $name = str_replace('/', '' ,$name);
          $obj['attr']  = array('id' => 'node_' . self::CORE, 'rel' => 'folder');
          $obj['data']  = $name;
          $obj['state'] = 'closed';
          $response[] = $obj;
          
          $name = str_replace(DIR_INCLUDES, '' ,DIR_BLOCKS);
          $name = str_replace('/', '' ,$name);
          $obj['attr']  = array('id' => 'node_' . self::BLOCKS, 'rel' => 'folder');
          $obj['data']  = $name;
          $obj['state'] = 'closed';
          $response[] = $obj;
          
          $name = str_replace(DIR_INCLUDES, '' ,DIR_CLASSES);
          $name = str_replace('/', '' ,$name);
          $obj['attr']  = array('id' => 'node_' . self::CLASSES, 'rel' => 'folder');
          $obj['data']  = $name;
          $obj['state'] = 'closed';
          $response[] = $obj;
          
          $name = str_replace(DIR_INCLUDES, '' ,DIR_STATIC_CLASSES);
          $name = str_replace('/', '' ,$name);
          $obj['attr']  = array('id' => 'node_' . self::ST, 'rel' => 'folder');
          $obj['data']  = $name;
          $obj['state'] = 'closed';
          $response[] = $obj;
          
        
        }
        
        if($node['id'] == self::CORE) {
          
          $classes    = iJScanDir(DIR_CORE);
          $interface  = iJScanDir(DIR_INTERFACES);
          $id = 100;
          foreach($classes as $class_name => $path) {
            
            if(!in_array($path, $interface)) {
              $obj['attr']  = array('id' => 'node_' . $id, 'rel' => 'class');
              $obj['data']  = ' ' . $class_name;
              $obj['state'] = '';
              $response[] = $obj;
            }  
          }
          
        }
        
        if($node['id'] == self::BLOCKS) {
          $obj['attr']  = array('id' => 'node_' . self::BACKEND_BLOCKS, 'rel' => 'folder');
          $obj['data']  = self::getNameFormPath(DIR_BACKEND_CLASSES);
          $obj['state'] = 'closed';
          $response[] = $obj;
          
          $obj['attr']  = array('id' => 'node_' . self::FRONTEND_BLOCKS, 'rel' => 'folder');
          $obj['data']  = self::getNameFormPath(DIR_FRONTEND_CLASSES);
          $obj['state'] = 'closed';
          $response[] = $obj;
        }
        
        
        if($node['id'] == self::BACKEND_BLOCKS) {
          $classes    = iJScanDir(DIR_BACKEND_CLASSES);
          $id = 100;
          foreach($classes as $class_name => $path) {
            $obj['attr']  = array('id' => 'node_' . $id, 'rel' => 'class');
            $obj['data']  = ' '  . $class_name;
            $obj['state'] = '';
            $response[] = $obj;
          }
        }
        
        if($node['id'] == self::FRONTEND_BLOCKS) {
          $classes    = iJScanDir(DIR_FRONTEND_CLASSES);
          $id = 100;
          foreach($classes as $class_name => $path) {
            $obj['attr']  = array('id' => 'node_' . $id, 'rel' => 'class');
            $obj['data']  = ' '  . $class_name;
            $obj['state'] = '';
            $response[] = $obj;
          }
        }
        
        if($node['id'] == self::CLASSES) {
          $classes    = iJScanDir(DIR_CLASSES);
          $id = 100;
          foreach($classes as $class_name => $path) {
            $obj['attr']  = array('id' => 'node_' . $id, 'rel' => 'class');
            $obj['data']  = ' '  . $class_name;
            $obj['state'] = '';
            $response[] = $obj;
          }
        }
        
        if($node['id'] == self::ST) {
          $classes    = iJScanDir(DIR_STATIC_CLASSES);
          $id = 100;
          foreach($classes as $class_name => $path) {
            $obj['attr']  = array('id' => 'node_' . $id, 'rel' => 'class');
            $obj['data']  = ' '  . $class_name;
            $obj['state'] = '';
            $response[] = $obj;
          }
        }
        
        
       
       // xDebug::log(json_encode($response));
        return json_encode($response);
        //return '[{"attr":{"id":"node_2","rel":"drive"},"data":"C:","state":"closed"},{"attr":{"id":"node_6","rel":"drive"},"data":"D:","state":""}]';  
    }
    
    
    public static function getNameFormPath($path) {
      $name  = trim($path, '/');
      $name  = strrchr($name, '/');  
      $name  = trim($name, '/');  
      return $name;
    }
    
    
  }
?>